package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.ChargeAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.marqueeview.MarqueeView;
import com.kj.xuyuan.model.ChargeBean;
import com.kj.xuyuan.model.ChargeBulletinBean;
import com.kj.xuyuan.pay.PayListenerUtils;
import com.kj.xuyuan.pay.PayResultListener;
import com.kj.xuyuan.pay.PayUtils;
import com.kj.xuyuan.pop.WithDrawPopWindow;
import com.kj.xuyuan.util.DisplayUtil;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.kj.xuyuan.widget.FullyGridLayoutManager;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 余额、充值
 */
public class ChargeActivity extends BaseActivity implements PayResultListener {

    @BindView(R.id.tv_top)
    MarqueeView tvTop;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.rv_recharge)
    RecyclerView rvRecharge;
    @BindView(R.id.iv_wx)
    ImageView ivWx;
    @BindView(R.id.ll_wx)
    LinearLayout llWx;
    @BindView(R.id.iv_ali)
    ImageView ivAli;
    @BindView(R.id.ll_ali)
    LinearLayout llAli;
    @BindView(R.id.tv_recharge)
    TextView tvRecharge;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.tv_withdraw_scale)
    TextView tvWithdrawScale;
    @BindView(R.id.ed_withdraw)
    EditText edWithdraw;

    private List<ChargeBean> data;
    private ChargeAdapter adapter;
    private int payType = 1;//1微信  2支付宝
    private int checkChargePosition = -1;
    private String balance,changeMoneyScale;//余额和提现比例

    public static void actionStar(Context context,String balance,String changeMoneyScale){
        Intent intent = new Intent(context,ChargeActivity.class);
        intent.putExtra("balance",balance);
        intent.putExtra("changeMoneyScale",changeMoneyScale);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_charge;
    }

    @Override
    protected void initView() {
        setTitleText("余额");
        setRightText("消费记录");
        balance = getIntent().getStringExtra("balance");
        changeMoneyScale = getIntent().getStringExtra("changeMoneyScale");
        tvBalance.setText(balance);
        tvWithdrawScale.setText("1:"+changeMoneyScale);
        PayListenerUtils.getInstance(context).addListener(this);
        //rvRecharge item  item_recharge
        initRecyclerView();
        getChargeData();
        getChargeBulletin();
    }

    private void getChargeBulletin() {
        OkGoUtil.postRequest(getContext(), UrlConstants.CHANGE_BULLETIN, null, new JsonCallback<BaseModel<List<ChargeBulletinBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<ChargeBulletinBean>>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        tvTop.startWithList(response.body().getData());
                    }
                }
            }

            @Override
            public void onError(Response<BaseModel<List<ChargeBulletinBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void getChargeData() {
        OkGoUtil.postRequest(getContext(), UrlConstants.GET_RECHARGE_LIST, null, new JsonCallback<BaseModel<List<ChargeBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<ChargeBean>>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        data.addAll(response.body().getData());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onError(Response<BaseModel<List<ChargeBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void initRecyclerView() {
        data = new ArrayList<>();
        adapter = new ChargeAdapter();
        rvRecharge.setLayoutManager(new FullyGridLayoutManager(context,3));
        rvRecharge.addItemDecoration(new GridSpacingItemDecoration(3, DisplayUtil.dp2px(context,8),false));
        rvRecharge.setAdapter(adapter);
        adapter.setNewData(data);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for(int i = 0;i<data.size();i++){
                    data.get(i).setSelected(false);
                }
                checkChargePosition = position;
                data.get(position).setSelected(true);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void rightTextViewClick() {
        super.rightTextViewClick();
        ConsumeHistoryActivity.actionStar(context);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    @OnClick({R.id.ll_wx, R.id.ll_ali, R.id.tv_recharge,R.id.tv_withdraw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_wx:
                if(payType != 1){
                    payType = 1;
                    ivWx.setVisibility(View.VISIBLE);
                    ivAli.setVisibility(View.GONE);
                }
                break;
            case R.id.ll_ali:
                if(payType != 2){
                    payType = 2;
                    ivWx.setVisibility(View.GONE);
                    ivAli.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.tv_recharge:
                if(checkChargePosition == -1){
                    ToastUtil.showToast(context,"未选择充值金额");
                    return;
                }
                if(payType == 2){
                    getAliOrder();
                }
                break;
            case R.id.tv_withdraw:
                popWithDrawWindow();
                break;
        }
    }

    private void popWithDrawWindow() {
        String money = edWithdraw.getText().toString().trim();
        if(StringUtils.isEmpty(money)){
            ToastUtil.showToast(context,"体现金额未填写");
            return;
        }
        WithDrawPopWindow popWindow = new WithDrawPopWindow(context);
        popWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER,0,0);
        popWindow.setAnimationStyle(R.style.popwindow_center_anim_style);
        popWindow.setWithdrawListener(new WithDrawPopWindow.WithdrawListener() {
            @Override
            public void withdrawListener(int withdrawType, String name, String account) {
                withdraw(withdrawType,name,account);
            }
        });
    }

    private void withdraw(int withdrawType, String name, String account) {
        Map map = new HashMap();
        map.put("type",withdrawType+"");
        map.put("num",edWithdraw.getText().toString().trim());
        map.put("account",account);
        map.put("name",name);
        OkGoUtil.postRequest(getContext(), UrlConstants.WITHDRAW, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        finish();
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void getAliOrder() {
        Map map = new HashMap();
        map.put("id",data.get(checkChargePosition).getId()+"");
        map.put("type",payType+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.CHARGE, map, new JsonCallback<BaseModel<String>>() {
            @Override
            public void onSuccess(Response<BaseModel<String>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        PayUtils.getInstance(context).toAliPay(response.body().getData());
                    }else{
                        ToastUtil.showToast(getContext(),response.body().getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<BaseModel<String>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PayListenerUtils.getInstance(context).removeListener(this);
    }

    @Override
    public void onPaySuccess() {
        finish();
    }

    @Override
    public void onPayError() {

    }

    @Override
    public void onPayCancel() {

    }
}